package com.huashengdou.generator.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huashengdou.generator.vo.ApiResult;
import com.huashengdou.generator.domain.User;
import com.huashengdou.generator.service.UserService;
import com.huashengdou.generator.vo.RequestUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.stream.Stream;

@Slf4j
@RestController
@RequestMapping("/user")
@Api(value = "UserAPI", tags = {"User"})
public class UserController {
    @Autowired
    private UserService userService;

    /**
    * 添加
    */
    @PostMapping("/add")
    @ApiOperation(value = "添加", response = ApiResult.class)
    public ApiResult<Boolean> add(@RequestBody User user) throws Exception {
        boolean flag = userService.save(user);
        return ApiResult.result(flag);
    }

    /**
    * 修改
    */
    @PostMapping("/update")
    @ApiOperation(value = "修改", response = ApiResult.class)
    public ApiResult<Boolean> update(@RequestBody User user) throws Exception {
        boolean flag = userService.saveOrUpdate(user);
        return ApiResult.result(flag);
    }

    /**
    * 删除
    */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "删除", response = ApiResult.class)
    public ApiResult<Boolean> delete(@PathVariable("id") Long id) throws Exception {
        boolean flag = userService.removeById(id);
        return ApiResult.result(flag);
    }

    /**
    * 批量删除
    * @param ids
    * @return
    * @throws Exception
    */
    @RequestMapping("/delByIds")
    @ApiOperation(value = "批量删除", response = ApiResult.class)
    public ApiResult<Boolean> delByIds(String ids) throws Exception {
        if (Objects.isNull(ids)) {
            return ApiResult.ok();
        }
        Stream.of(ids.split(",")).forEach(id -> {
            userService.removeById(Long.valueOf(id));
        });
        return ApiResult.ok();
    }

    /**
    * 获取详情
    */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "用户", response = User.class)
    public ApiResult<User> getUser(@PathVariable("id") Long id) throws Exception {
        User user = userService.getById(id);
        return ApiResult.ok(user);
    }

    /**
    * 分页列表
    */
    @PostMapping("/getPageList")
    @ApiOperation(value = "分页列表", response = User.class)
    public ApiResult<IPage<User>> getUserPageList(@RequestBody RequestUser queryUser) throws Exception {
        Page<User> queryPage = Page.of(queryUser.getPage(), queryUser.getLimit());
        Wrapper<User> queryWapper = new QueryWrapper<>();
        IPage<User> page = userService.page(queryPage,queryWapper);
        return ApiResult.ok(page);
    }
}